package stanaGUI;
import java.util.List;

import javax.swing.JTable;
import javax.swing.text.TabExpander;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeTableXYDataset;
import org.jfree.data.xy.DefaultTableXYDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.util.Rotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import stanaGUI.KMeans.Cluster;

import stanaGUI.Hierarchical.*;
import stanaGUI.KMeans.*;
public class GraphicalRepresentationKmeans {
	
	public ChartPanel PieChart (
			String applicationTitle, 
			String chartTitle,
			List<Cluster> clusters2
			) {
		DefaultPieDataset result = new DefaultPieDataset();
        // This will create the dataset 
        PieDataset dataset = createPieDataset(result,clusters2);
       
        // based on the dataset we create the chart
        JFreeChart chart = createPieChart(dataset, chartTitle);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        // add it to our application
         return chartPanel;

    }
	
   public ChartPanel Histogram(String title, List<stanaGUI.KMeans.Cluster> list) {
        IntervalXYDataset dataset = createHistogramDataset(list);
        JFreeChart chart = createHistogramChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        return chartPanel;
   }
   
   public ChartPanel AreaChart(String title, List<stanaGUI.KMeans.Cluster> list) {

	   DefaultTableXYDataset dataset = createAreaChartDataset(list);
       JFreeChart chart = createAreaChart(dataset, title);
       ChartPanel chartPanel = new ChartPanel(chart);
       chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
       chartPanel.setMouseZoomable(true, false);
       return chartPanel;
  }

   
   // This is the way the scatter Chart is getting created
   public ChartPanel ScatterChart(String title, List<stanaGUI.KMeans.Cluster> list) {

	   DefaultTableXYDataset dataset = createAreaChartDataset(list);
       JFreeChart chart = createScatterChart(dataset, title);
       ChartPanel chartPanel = new ChartPanel(chart);
       chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
       chartPanel.setMouseZoomable(true, false);
       return chartPanel;
  }
   /**
    * Creates a chart
    */
   private JFreeChart createAreaChart(DefaultTableXYDataset dataset , String title) {
 

	   JFreeChart jfreechart = ChartFactory.createStackedXYAreaChart(
			   "Stacked Two variables Area Chart", 
			   "X Value", 
			   "Y Value", 
			   dataset, 
			   PlotOrientation.VERTICAL, 
			   true, 
			   true, 
			   false);
	   
       XYPlot xyplot = (XYPlot)jfreechart.getPlot();   
       StackedXYAreaRenderer2 stackedxyarearenderer2 = new StackedXYAreaRenderer2();   
       stackedxyarearenderer2.setToolTipGenerator(new StandardXYToolTipGenerator());   
       xyplot.setRenderer(0, stackedxyarearenderer2);   
       return jfreechart;         
       
   }
	
 
    /**
     * Creates a chart
     */
    private JFreeChart createPieChart(PieDataset dataset, String title) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(title,  				// chart title
            dataset,                // data
            true,                   // include legend
            true,
            false);

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
        
    }
	
    private JFreeChart createScatterChart(final XYDataset dataset, String title) {
        final JFreeChart chart = ChartFactory.createScatterPlot(
            title,                  // chart title
            "X",                      // x axis label
            "Y",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        );
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
        plot.setRenderer(renderer);
        return chart;
    }

    
    
    /**
     * Creates a Histogram chart.
     * 
     * @param dataset  a dataset.
     * 
     * @return The chart.
     */
    private JFreeChart createHistogramChart(IntervalXYDataset dataset) {
        JFreeChart chart = ChartFactory.createHistogram(
            "Histogram", 
            null, 
            null, 
            dataset, 
            PlotOrientation.VERTICAL, 
            true, 
            false, 
            false
        );
        chart.getXYPlot().setForegroundAlpha(0.75f);
        
        final ChartPanel panel = new ChartPanel(chart);
        
        return chart;
    }
    
    
    
    /**
     * Creates a sample dataset 
     */
    private  PieDataset createPieDataset(DefaultPieDataset result, List<stanaGUI.KMeans.Cluster> clusters2) {
    	// Now we need to add the dataset values
    	int i = 0;
    	for (stanaGUI.KMeans.Cluster cluster1 : clusters2) {
    		//percent in cluster
    		JTable tableValues = Welcome.dataTableValue;
    		int index1 = Welcome.index1;
    		int index2 = Welcome.index2;
    		int n=0;
    		String label = "(";
    		//System.out.println("so row" + tableValues.getRowCount());
    		for(int m=0;m<tableValues.getRowCount();m++){
    			//System.out.println(cluster1.getItems().get(2).value1+"=="+Double.parseDouble((String) tableValues.getValueAt(m,index1)));
    			if(cluster1.getItems().get(2).value1==Double.parseDouble((String) tableValues.getValueAt(m,index1))&&cluster1.getItems().get(2).value2==Double.parseDouble((String) tableValues.getValueAt(m,index2))){
    				for(int k = 0;k<tableValues.getColumnCount();k++){
    					if(k<tableValues.getColumnCount()-1)
    					label = label + tableValues.getValueAt(m,k)+" - ";
    					if(k==tableValues.getColumnCount()-1)
    					label = label + tableValues.getValueAt(m,k)+")(";	
    				}
    				
    			n++;
    			}
    			if(cluster1.getItems().get(3).value1==Double.parseDouble((String) tableValues.getValueAt(m,index1))&&cluster1.getItems().get(3).value2==Double.parseDouble((String) tableValues.getValueAt(m,index2))){
    				for(int k = 0;k<tableValues.getColumnCount();k++){
    					if(k<tableValues.getColumnCount()-1)
        					label = label + tableValues.getValueAt(m,k)+" - ";
        					if(k==tableValues.getColumnCount()-1)
        					label = label + tableValues.getValueAt(m,k)+")";
    				}
    				
    			n++;
    			}
    			if(n==2) break;
    		}
    		
    		result.setValue("cluster"+i+" "+label,cluster1.size());
    			
    		/*for (stanaGUI.KMeans.Item item : cluster1.getItems()) {
				result.setValue(Double.toString(item.getValue1()), item.getValue2());
			}*/
			
    		i++;
    	}
    	return result;
        
    }
    /**
     * Creates a sample {@link HistogramDataset}.
     * 
     * @return The dataset.
     */
    private IntervalXYDataset createHistogramDataset(List<stanaGUI.KMeans.Cluster> clusters) {
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.RELATIVE_FREQUENCY);
        int i = 0;
        int numOfItems = clusters.size();
        //int numOfItems = 16; //1000000;//Don't care size of clusters :D
        //System.out.println("num of Item= "+numOfItems);
        
        double[] quantity = new double[numOfItems];
        double[] prices = new double[numOfItems];
        
       	for (stanaGUI.KMeans.Cluster cluster1 : clusters) {
       		
    			for (stanaGUI.KMeans.Item item : cluster1.getItems()) {
    				if(i< numOfItems){
    				//System.out.println("value1: "+item.getValue1());
    				//System.out.println("value2: "+item.getValue2());
    				//System.out.println("i ="+i);
    				quantity[i] = item.getValue1();
    				prices[i] = item.getValue2();
    				i++;
    				}
    				else break;
    			}
       	}
        dataset.addSeries("Value1 (quantity) ", quantity, 1);
        dataset.addSeries("Value2 (prices) ", prices, 1);   
        return dataset;     
    }
    
    /**
     * Creates a sample {@link HistogramDataset}.
     * 
     * @return The dataset.
     */
    private DefaultTableXYDataset createAreaChartDataset(List<stanaGUI.KMeans.Cluster> clusters) {
    	DefaultTableXYDataset dataset = new  DefaultTableXYDataset();
    	//XYSeries dataSeries = new XYSeries(null); //phat change
    	XYSeries dataSeries = new XYSeries(1);
  
        int i = 0;
        int j = 0;
        int numOfItems = clusters.size();
        final double[][] data = new double[numOfItems][numOfItems];
       	for (stanaGUI.KMeans.Cluster cluster1 : clusters) {
    			for (stanaGUI.KMeans.Item item : cluster1.getItems()) {
    				if(i<numOfItems){
    				dataSeries.add(item.getValue1(), item.getValue2());
      				i++;
    				}
    				else break;
    			}
    			//if(j>= numOfItems) break;
    			//else j++;
       	}
       	dataset.addSeries(dataSeries);
       	return dataset;     
    }
    /**
     * Creates a sample {@link HistogramDataset}.
     * 
     * @return The dataset.
     */
    private DefaultTableXYDataset createScatterDataset(List<stanaGUI.KMeans.Cluster> clusters) {
    	DefaultTableXYDataset dataset = new  DefaultTableXYDataset();
    	XYSeries dataSeries = new XYSeries(null);
  
        int i = 0;
        int j = 0;
        int numOfItems = clusters.size();
        final double[][] data = new double[numOfItems][numOfItems];
       	for (stanaGUI.KMeans.Cluster cluster1 : clusters) {
    			for (stanaGUI.KMeans.Item item : cluster1.getItems()) {
    				dataSeries.add(item.getValue1(), item.getValue2());
      				i++;
    			}
    			j++;
       	}
       	dataset.addSeries(dataSeries);
       	return dataset;     
    }
    
}